package com.melly.demo1.utils;


import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey; 
import java.security.interfaces.RSAPublicKey; 
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;

/**
 * 
 * @author gengkangkang
 *
 */
public class EncryptionUtils {
	
	public  String testgkk(){
		return "gkk test secure";
	}
	
	 /**
     * 加密
     */
    private Cipher encryptCipher;

    /**
     * 解密
     */
    private Cipher decryptCipher;

    /**
     * KeyFactory
     */
    private SecretKeyFactory keyFactory;

    public  EncryptionUtils() {
        try {
            encryptCipher = Cipher.getInstance("DES");
            decryptCipher = Cipher.getInstance("DES");
            keyFactory = SecretKeyFactory.getInstance("DES");
        } catch (GeneralSecurityException ex) {
            System.out.println("I don't think this will happen...");
        }
    }

    public void init(String salt) {
        try {
            SecretKey sk = keyFactory.generateSecret(new DESKeySpec(salt.getBytes()));
         //   System.out.println("秘钥=="+sk.toString());
            encryptCipher.init(Cipher.ENCRYPT_MODE, sk);
            decryptCipher.init(Cipher.DECRYPT_MODE, sk);
        } catch (InvalidKeyException | InvalidKeySpecException ex) {
            System.out.println("Can't init Cipher.[salt={}]");
        }
    }

    public  String encrypt(String value) throws GeneralSecurityException {
        return new String(Base64.encodeBase64(encryptCipher.doFinal(value.getBytes())));
    }

  
    public String decrypt(String value) throws GeneralSecurityException {
        return new String(decryptCipher.doFinal(Base64.decodeBase64(value.getBytes())));
    }
    
    
    ///////////////////////////////////////////////rsa
    
    /**
     * 取得RSA私钥
     * @param keyMap
     * @return
     * @throws Exception
     */
    public static byte[] getRSAPrivateKey(Map<String,Object> keyMap) throws Exception{
    	Key key=(Key)keyMap.get(Constants.RSA_PRIVATE_KEY);
    	return key.getEncoded();
    }
    /**
     * 取得RSA公钥
     * @param keyMap
     * @return
     * @throws Exception
     */
    public static byte[] getRSAPublicKey(Map<String,Object> keyMap) throws Exception{
    	Key key=(Key)keyMap.get(Constants.RSA_PUBLIC_KEY);
    	return key.getEncoded();
    } 
    /**
     * RSA密钥初始化
     * @return
     * @throws Exception
     */
    public static Map<String,Object> initRSAKey() throws Exception{
    	//实例化密钥对生成器
    	KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance(Constants.RSA_KEY_ALGORITHM);
    	//初始化密钥对生成器
    	keyPairGen.initialize(Constants.RSA_KEY_SIZE);
    	//生成密钥对
    	KeyPair keyPair=keyPairGen.generateKeyPair();
    	//公钥
    	RSAPublicKey publicKey=(RSAPublicKey)keyPair.getPublic();
    	RSAPrivateKey privateKey=(RSAPrivateKey)keyPair.getPrivate();
    	//封装密钥
    	Map<String,Object> keyMap=new HashMap<String,Object>(2);
    	keyMap.put(Constants.RSA_PUBLIC_KEY, publicKey);
    	keyMap.put(Constants.RSA_PRIVATE_KEY, privateKey);
		return keyMap;
    	
    }
    
	
	public static void main(String[] args) throws Exception{
//		System.out.println("*********************************des****************************************");
		  String salt="CreditCloudRock!";
		  EncryptionUtils t=new EncryptionUtils();
		  t.init(salt);
//		  13916670845,18817327740,13654325432
		  String m="18017968792";
		  String value="qGONSXst0JWhnoXUvsGTFw==";
                  String[] te = {"qGONSXst0JWhnoXUvsGTFw=="};
                  for(int i=0;i<te.length;i++){
                      String test = te[i];
                      String no=t.decrypt(test);
                      System.out.println(no);
                  }
		  String mobile=t.encrypt(m);
		  System.out.println(m+":"+mobile);

	}
}
